import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart' as flutter_acrylic;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:system_theme/system_theme.dart';
import 'package:window_manager/window_manager.dart';

import '../views/app/app_theme.dart';
import '../views/app/app_view_main.dart';
import '../views/app/app_view_model.dart';
import 'global_setting.dart';

/// Checks if the current environment is a desktop environment.
bool get isDesktop {
  if (kIsWeb) return false;
  return [
    TargetPlatform.windows,
    TargetPlatform.linux,
    TargetPlatform.macOS,
  ].contains(defaultTargetPlatform);
}

///主函数入口
void main() async {
  //初始化本地存储
  await globalSetting.loadLocal();
  // if it's not on the web, windows or android, load the accent color
  if (!kIsWeb &&
      [TargetPlatform.windows, TargetPlatform.android]
          .contains(defaultTargetPlatform)) {
    SystemTheme.accentColor.load();
  }
  //desktop
  if (isDesktop) {
    //毛玻璃效果初始化
    await flutter_acrylic.Window.initialize();
    if (defaultTargetPlatform == TargetPlatform.windows) {
      await flutter_acrylic.Window.hideWindowControls();
    }
    //窗口管理器初始化
    await WindowManager.instance.ensureInitialized();
    windowManager.waitUntilReadyToShow().then((_) async {
      await windowManager.setTitleBarStyle(
        TitleBarStyle.hidden,
        windowButtonVisibility: false,
      );
      await windowManager.setMinimumSize(const Size(500, 600));
      await windowManager.show();
      await windowManager.setPreventClose(true);
      await windowManager.setSkipTaskbar(false);
    });
  }
  //初始化screentUtil
  await ScreenUtil.ensureScreenSize();
  //应用入口
  runApp(MultiProvider(
    providers: [
      //主题
      ChangeNotifierProvider<AppTheme>(create: (_) => AppTheme()),
      //应用
      ChangeNotifierProvider<AppViewModel>(create: (_) => AppViewModel())
    ],
    child: const DragToMoveArea(child: AppViewMain()),
  ));
}
